using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using HospitalTracking.Interfaces;
using System;
using HospitalTracking.Model;
using System.Windows;
using GalaSoft.MvvmLight.Command;

namespace HospitalTracking.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private IHospitalService hospitalService;
        private Hospital hospital;

        public Hospital Hospital 
        {
            get { return hospital; }

            set
            {
                if (hospital != value)
                {
                    hospital = value;
                    RaisePropertyChanged("Hospital");
                }
            }
        }


        public RelayCommand LoadHospitalDataCommand 
        { 
            get; 
            private set; 
        }

        

        
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IHospitalService hospitalService)
        {
            this.hospitalService = hospitalService;
            if (IsInDesignMode)
            {
                this.hospitalService.GetHospitalData(GetHospitalDataCompleted);
                // Code runs in Blend --> create design time data.
            }
            else
            {
                LoadHospitalDataCommand = new RelayCommand(
              () =>
              {
                  this.hospitalService.GetHospitalData(GetHospitalDataCompleted);
              });

            }
            
        }

        private void GetHospitalDataCompleted(Hospital result, Exception error)
        {
            if (error != null)
            {
                MessageBox.Show(error.Message);
                return;
            }

            if (result == null)
            {
                MessageBox.Show("Nothing found");
                return;
            }
            this.Hospital = result;
        }
    }
}